﻿using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
//susing Microsoft.Expression.Interactivity.Core;
using System.Windows;
using System.IO;
using GalaSoft.MvvmLight.Command;
using System;

namespace playlist
{
    class PlayListViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        #region ATTRIBUTES

        private List<string> playLists_;
        private List<string> mediaPlayList_;
        private List<string> dirSearcher_;
        private int mediaPlayListsIndex_;
        private int playListsIndex_;
        private PlayListsModel playListsModel_;
        private string mediaFileName_;
        
        #endregion ATTRIBUTES
        
        #region COMMAND_RELAYERS

        public RelayCommand ListCmd { get; set; }
        public RelayCommand PlayCmd { get; set; }
        public RelayCommand PauseCmd { get; set; }
        public RelayCommand SelectPlayListCmd { get; set; }
        public RelayCommand DeletePlayListCmd { get; set; }
        public RelayCommand AddPlayListCmd { get; set; }
        public RelayCommand CloseWindowCmd { get; set; }
        public RelayCommand<DragEventArgs> DragCmd { get; private set; }
        
        #endregion
        
        #region PROPERTIES

        public string MediaFilePath
        {
            get { return mediaFileName_; }
            set
            {
                if (mediaFileName_ != value)
                {
                    mediaFileName_ = value;
                    OnPropertyChanged("MediaFilePath");
                }
            }
        }


        public List<string> MediaPlayList
        {
            get { return this.mediaPlayList_; }
            set
            {
                List<string> l = new List<string>(value);
                for (int i = 0; i < l.Count; i++)
                    l[i] = Path.GetFileNameWithoutExtension(l[i]);
               
                if (this.mediaPlayList_ != l)
                {
                    this.mediaPlayList_ = l;
                    OnPropertyChanged("MediaPlayList");
                }
            }
        }

        public List<string> PlayLists
        {
            get { return this.playLists_; }
            set
            {
                if (this.playLists_ != value)
                {
                    this.playLists_ = value;
                    OnPropertyChanged("PlayLists");
                }
            }
        }

        public int MediaPlayListIndex
        {
            get { return this.mediaPlayListsIndex_; }
            set
            {
                if (mediaPlayListsIndex_ != value)
                {
                    mediaPlayListsIndex_ = value;
                    OnPropertyChanged("MediaPlayListIndex");
                }
            }
        }

        public int PlayListsIndex
        {
            get { return this.playListsIndex_; }
            set
            {
                if (playListsIndex_ != value)
                {
                    playListsIndex_ = value;
                    OnPropertyChanged("PlayListsIndex");
                }
            }
        }

        #endregion

        #region EVENTS

        public event EventHandler Play;
        public event EventHandler Pause;

        #endregion

        #region CTOR
        public PlayListViewModel()
        {
            this.playListsModel_ = new PlayListsModel("");
            this.PlayLists = this.playListsModel_.PlayListsDic.Keys.ToList();
            this.MediaPlayList = this.playListsModel_.PlayListsDic.First().Value.MediaDic.Keys.ToList();
            this.PlayListsIndex = 0;
            this.MediaPlayListIndex = 0;
            this.MediaFilePath = "C:\\Users\\Public\\Videos\\Sample Videos\\Wildlife.wmv";



            this.PauseCmd = new RelayCommand(e =>
            {
                Pause.Invoke(this, EventArgs.Empty);
            });

            this.PlayCmd = new RelayCommand(e =>
            {
               if (this.MediaPlayListIndex > -1)
                {
                    MediaFilePath = this.playListsModel_.PlayListsDic.ElementAt(PlayListsIndex).Value.MediaDic.ElementAt(MediaPlayListIndex).Key;
                    if (Play != null)
                        Play.Invoke(this, EventArgs.Empty);
                }
                Play.Invoke(this, EventArgs.Empty);
            });


            this.ListCmd = new RelayCommand(e =>
            {
              //  Pause.Invoke(this, EventArgs.Empty);
            });

            this.DragCmd = new RelayCommand<DragEventArgs>(e =>
            {
                if (e.Data == null)
                    return;

                var files = e.Data.GetData(DataFormats.FileDrop, true)
                   as string[];

                if (files == null || files.Length == 0)
                    return;
                List<string> concat = new List<string>();
                this.dirSearcher_ = new List<string>();
                foreach (string file in files)
                {
                    if (this.IsADirectory(file))
                    {
                        this.DirSearch(file);
                        concat = concat.Concat(this.dirSearcher_).ToList();
                    }
                    else if (this.IsAMediaFile(file))
                    {
                         concat.Add(file);
                    }
                }
                if (this.PlayListsIndex > -1)
                {
                    this.playListsModel_.AddInPlayList(this.PlayListsIndex, concat);
                    this.MediaPlayList = this.playListsModel_.PlayListsDic.ElementAt(this.PlayListsIndex).Value.MediaDic.Keys.ToList();
                }
            });

            this.SelectPlayListCmd = new RelayCommand(e =>
            {
                if (this.PlayListsIndex > -1)
                this.MediaPlayList = this.playListsModel_.PlayListsDic.ElementAt(this.PlayListsIndex).Value.MediaDic.Keys.ToList();
            });

            this.CloseWindowCmd = new RelayCommand(e =>
            {
                this.playListsModel_.Save();
            });

            this.DeletePlayListCmd = new RelayCommand(e =>
            {
                this.playListsModel_.PlayListsDic.Remove(this.playListsModel_.PlayListsDic.ElementAt(this.PlayListsIndex).Key);
                if (this.playListsModel_.PlayListsDic.Count == 0)
                {
                    this.playListsModel_.AddDefaultPlaylist();
                }
                this.PlayLists = this.playListsModel_.PlayListsDic.Keys.ToList();
                this.MediaPlayList = this.playListsModel_.PlayListsDic.First().Value.MediaDic.Keys.ToList();
                this.PlayListsIndex = 0;
                this.MediaPlayListIndex = 0;
            });

            this.AddPlayListCmd = new RelayCommand(e =>
            {
                this.playListsModel_.AddDefaultPlaylist(this.playListsModel_.PlayListsDic.Count());
                this.PlayLists = this.playListsModel_.PlayListsDic.Keys.ToList();
                this.MediaPlayList = this.playListsModel_.PlayListsDic.First().Value.MediaDic.Keys.ToList();
                this.PlayListsIndex = 0;
                this.MediaPlayListIndex = 0;
            });

        }
        #endregion


        #region METHODS
        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
        }

        private void DirSearch(string sDir)
        {
            string[] arrFolder = { };
            try
            {
                arrFolder = Directory.GetDirectories(sDir);
            }
            catch { }

            foreach (string fo in arrFolder)
            {
                try
                {
                    foreach (string f in Directory.GetFiles(fo))
                    {
                        if (IsAMediaFile(f))
                        {
                            this.dirSearcher_.Add(f);
                        }
                   }
                    DirSearch(fo);
                }
                catch { }
            }
        }

        private bool IsADirectory(string path)
        {
            bool bIsDirectory = true;

            try
            { string[] subfolders = Directory.GetDirectories(path); }
            catch (System.IO.IOException)
            { bIsDirectory = false; }
            return bIsDirectory;
        }

        private bool IsAMediaFile(string file)
        {
            string sPattern = "\\.mp3|\\.wmv|\\.avi|\\.jpg|\\.bmp|\\.png";
            if (System.Text.RegularExpressions.Regex.IsMatch(file, sPattern))
               return true;
            return false;
        }

        #endregion

    }
}
